<style scoped>
.layout{
    border: 1px solid #d7dde4;
    background: #f5f7f9;
    position: relative;
    border-radius: 4px;
    overflow: hidden;
}
.layout-logo{
    width: 100px;
    height: 30px;
    background: #5b6270;
    border-radius: 3px;
    float: left;
    position: relative;
    top: 15px;
    left: 20px;
}
.layout-nav{
    width: 420px;
    margin: 0 auto;
    margin-right: 20px;
}
</style>
<template>
    <div class="layout">
        {{ token }}
        <div v-if='!login'>
            <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
        <FormItem prop="user">
            <Input type="text" v-model="formInline.user" placeholder="Username">
                <Icon type="ios-person-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem prop="password">
            <Input type="password" v-model="formInline.password" placeholder="Password">
                <Icon type="ios-lock-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formInline')">Signin</Button>
        </FormItem>
    </Form>
        </div>
        <div v-else>
            <Layout>
            <Header>
                <Menu mode="horizontal" theme="dark" active-name="1">
                    <div class="layout-logo"></div>
                    <div class="layout-nav">
                        <MenuItem name="1" style="font-size:26px">
                            <Icon type="ios-navigate"></Icon>
                            后台管理系统
                        </MenuItem>

                    </div>
                </Menu>
            </Header>
            <Layout>
                <Sider hide-trigger :style="{background: '#fff'}">
                    <Menu active-name="1-2" theme="light" width="auto" >

                            <MenuItem name="1-1"><router-link to="/">平台首页</router-link></MenuItem>
                            <MenuItem name="1-2"><router-link to="/users">用户管理</router-link></MenuItem>
                            <MenuItem name="1-3"><router-link to="/cates">分类管理</router-link></MenuItem>
                            <MenuItem name="1-4"><router-link to="/products">产品管理</router-link></MenuItem>
                            <MenuItem name="1-5"><router-link to="/orders">订单管理</router-link></MenuItem>
                             <MenuItem name="1-6"><router-link to="/news">新闻管理</router-link></MenuItem>
                            <MenuItem name="1-7"><router-link to="/about">关于我们</router-link></MenuItem>


                    </Menu>
                </Sider>
                <Layout :style="{padding: '0 24px 24px'}">
                    <Content :style="{padding: '24px', minHeight: '280px', background: '#fff'}">
                        <router-view></router-view>
                    </Content>
                </Layout>
            </Layout>
        </Layout>
    </div>
</div>
</template>
<script>
    import {mapGetters,mapActions} from 'vuex';
    export default {
        data () {
            return {
                formInline: {
                    user: '',
                    password: ''
                },
                ruleInline: {
                    user: [
                        { required: true, message: 'Please fill in the user name', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: 'Please fill in the password.', trigger: 'blur' },
                        { type: 'string', min: 6, message: 'The password length cannot be less than 6 bits', trigger: 'blur' }
                    ]
                }
            }
        },
        computed:{
            ...mapGetters(['login','token'])
        },
        methods:{
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {

                       this.axios({
                           url:'http://localhost:3000/users/login',
                           method:'post',
                       }).then(res=>{
                           this.setToken(res.data.token);
                           this.checkLogin(true);
                       })
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            },
            ...mapActions(['checkLogin','setToken'])
        }
    }
</script>
